package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;


    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMapper.selectCountByPhone(phone);
        return count>0;
    }

    @Override
    public void sendVerifyCode(String phone) throws LogicException{
        //通过UUID随机生成验证码,通过replaceAll将生成的"-"替换
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0,4);

        //判断是否一分钟发送请求超过3次
        Long count = userInfoRedisService.verifyCodeLimitTime(phone);
        System.out.println(count);
        if(count > 3L){
            throw new LogicException("您的操作太频繁了");
        }

        //创建StringBuilder拼接验证码信息
        StringBuilder sb = new StringBuilder(100);
        sb.append("您的验证码为：").append(code).append(",请在")
                .append(Consts.VERIFY_CODE_VAI_TIME)
                .append("分钟之内进行注册操作。");
        String value = code;
        System.out.println(sb);

        //将验证码信息缓存到redis中
        userInfoRedisService.saveVerifyCode(phone,value);
    }



    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException {

        //判断参数是否为空
        AssertUtil.hasLength(phone,"手机号不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        //判断两次密码是否一致
        AssertUtil.isEquals(password,rpassword,"两次密码不一致");
        //判断手机格式是否正确 【拓展】
        //判断手机是否唯一
        if(this.checkPhone(phone)){
            throw new LogicException("手机号已被注册");
        }

        //判断验证码是否正确
        String code = userInfoRedisService.getVerifyCode(phone);
        System.out.println(code);
        if(!verifyCode.equals(code)){
            throw new LogicException("验证码失效或错误");
        }

        //进行注册
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setPassword(password);//密码需要进行加密处理
        userInfo.setNickname(nickname);
        userInfo.setHeadImgUrl("/images/default.png");
        userInfo.setLevel(1);
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfoMapper.insert(userInfo);
    }

    @Override
    public String userLogin(String username, String password) throws LogicException{
        //检查账号和密码是否正确
        UserInfo userInfo =  userInfoMapper.selectUserByNamePwd(username,password);

        //判断用户信息是否为空
        if(userInfo == null){
            throw new LogicException("账号或密码不存在");
        }

        //用户信息存在:通过UUID创建出一个token值
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        //把token作为redis的key,用户信息作为redis的value缓存到redis中
        userInfoRedisService.setUserByToken(token,userInfo);

        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
